import { useRouter } from 'vue-router'
import store from "../store";
import Bus from "@/utils/eventBus";

export function appGetTimeList(start: any, end: any, interval_times: any) {
    let temStart = start
    let list = [start]
    while (temStart < end) {
        temStart = temStart + interval_times

        list.push(temStart)
    }
    return list
}

/* 设置自动巡检数据 */
let autoCheckTimeList: any = []
export function setAutoCheckData(list: any,sTime:number) {
    autoCheckTimeList = []
    let serverTime_ = sTime
    let serverTime = new Date(sTime * 1000)
    let currentHH = serverTime.getHours()
    let currentmm = serverTime.getMinutes()
    let currentYYMMDD = `${serverTime.getFullYear()}/${serverTime.getMonth() + 1}/${serverTime.getDate()}`
    list.forEach((v: any) => {
        let begin = parseInt(String(+new Date(`${currentYYMMDD} ${v.begin}:00`) / 1000))
        let end = parseInt(String(+new Date(`${currentYYMMDD} ${v.end}:00`) / 1000))
        let timeList: any = []
        if (serverTime_ >= begin && serverTime_ < end) {
            appGetTimeList(serverTime_, end, v.interval_times * 60).forEach(time => {
                timeList.push({
                    time: time, //时间戳
                    times: v.times,
                    camera_list: v.camera_list.split(','),
                })
            })
        }
        if (serverTime_ < begin) {
            appGetTimeList(begin, serverTime_, v.interval_times * 60).forEach(time => {
                timeList.push({
                    time: time, //时间戳
                    times: v.times,
                    camera_list: v.camera_list.split(','),
                })
            })
        }
        autoCheckTimeList = autoCheckTimeList.concat(timeList)
    })

}
Bus.$on("serverTime", (time: any) => {
    let find = autoCheckTimeList.find((v:any)=>v.time == time)
    // console.log(find)
    if(find){
        setAutoCheckALert(find)
    }
})

export function setAutoCheckALert(find: any){
    let closeTime = 0
    /* 开启 */
    find.camera_list.forEach((v:any,i:number)=>{
        closeTime = find.times * 60 * 1000 * i + find.times * 60 * 1000
        setTimeout(()=>{
            //打开视频
            Bus.$emit("openVideo",{})
        },find.times *  60 * 1000 * i)
    })
    console.log(closeTime)
    /* 关闭 */
    setTimeout(() => {
        Bus.$emit("closeVideo",{})
        // this.videoDialogVisible = false;
        // if (document.getElementById("qt_device") != null) {
        //     cpp.closeDialog()
        // } else {
        //     appVue.$message.error('请使用app查看');
        // }
    },closeTime - 1000)
}
